//
//  RHMEPricePatternDetailVC.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/3/3.
//

#import "RHMEPricePatternDetailVC.h"
#import "RHMePriceCityCell.h"
#import "RHMePriceCell.h"
#import "RHMePriceTimeRangeCell.h"
#import "RHPlantModel.h"
#import "RHMeElectricPricePatternModel.h"

@interface RHMEPricePatternDetailVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) NSArray *dataArr;

@property (nonatomic ,strong) NSMutableArray *timeArr;
@end

@implementation RHMEPricePatternDetailVC


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"模板详情".rh_localized;
    self.view.backgroundColor = CFTabViewBgColor;
    [self createData];
    [self setupUI];
    NSDictionary *dic = [(NSObject*)self.patternModel yy_modelToJSONObject];
    NSString *configString = dic[@"configJson"];
    NSArray *arr = [configString jsonStringToArray];
    self.timeArr = arr.mutableCopy;
    [self readData:dic];
}

- (void)setupUI{
    kWeakSelf;
    self.timeArr = [NSMutableArray array];
    QMUIButton *sureBtn = [QMUIButton buttonWithTitle:@"使用模板".rh_localized titleColor:UIColor.whiteColor titleFont:kBOLDSYSTEMFONT(20)];
    [sureBtn setCornerRadius:8];
    [sureBtn setBackgroundColor:CMainColorGreen];
    [sureBtn addTarget:self action:@selector(userPatternAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.offset(50);
        make.bottom.mas_offset(kSafe_bottom>0?-kSafe_bottom:-15);
    }];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [_tableView setBackgroundColor:kCOLOR_HEX(@"#F0F5FF")];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setSeparatorColor:UIColor.clearColor];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView registerClass:[RHMePriceCell class] forCellReuseIdentifier:@"RHMePriceCell_detail"];
    [_tableView registerClass:[RHMePriceTimeRangeCell class] forCellReuseIdentifier:@"RHMePriceTimeRangeCell_detail"];
    [self.view addSubview:_tableView];
   
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.bottom.equalTo(sureBtn.mas_top).offset(-15);
        make.top.mas_offset(0);
    }];

}

-(void)userPatternAction
{
  
    if (self.userPatternBlock) {
        self.userPatternBlock(self.patternModel);
        [self.navigationController popViewControllerAnimated:NO];
    }
}

-(void)readData:(NSDictionary*)dic
{
    [self.dataArr enumerateObjectsUsingBlock:^(NSArray*  _Nonnull sectionArr, NSUInteger idx, BOOL * _Nonnull stop) {
        [sectionArr enumerateObjectsUsingBlock:^(RHMeElectricPricePatternModel*  _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
            [model readDic:dic];
        }];
        
    }];
    [self.tableView reloadData];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
   
    if (section ==0 || section == 1) {
        return 55;
    }
    return 15;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section ==0 || section == 1) {
        UIView *headerView = kView;
        UIView *bgView = kView;
        [headerView addSubview:bgView];
        bgView.backgroundColor = UIColor.whiteColor;
        [bgView roundBorder:8 width:1 color:kBorderColor RectCorners:QMUIViewBorderPositionLeft|QMUIViewBorderPositionTop|QMUIViewBorderPositionRight];
        bgView.layer.qmui_maskedCorners = QMUILayerMinXMinYCorner|QMUILayerMaxXMinYCorner;
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_offset(15);
            make.left.mas_offset(15);
            make.right.mas_offset(-15);
            make.bottom.mas_offset(0);
        }];
        
        QMUILabel *titleLab = [QMUILabel labelWithText:section==0?@"充电电价".rh_localized:@"放电电价".rh_localized font:kBOLDSYSTEMFONT(16) textColor:kCOLOR_HEX(@"#303133")];
        [bgView addSubview:titleLab];
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.centerY.mas_offset(0);
        }];
        [headerView addSubview:bgView];
        
        return headerView;
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
    NSArray *sectionArr = [self.dataArr safeObjectAtIndex:section];
    return sectionArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *sectionArr = [self.dataArr safeObjectAtIndex:indexPath.section];
    RHMeElectricPricePatternModel *model = [sectionArr safeObjectAtIndex:indexPath.row];
    if([model.tag isEqualToString:@"pattern_time"]){//时段
        RHMePriceTimeRangeCell *timeCell = [tableView dequeueReusableCellWithIdentifier:@"RHMePriceTimeRangeCell_detail"];
        timeCell.selectionStyle = UITableViewCellSelectionStyleNone;
        timeCell.timeArr = self.timeArr;
        return timeCell;
    }else if([model.tag isEqualToString:@"pattern_price"]){
        RHMePriceCell *priceCell = [tableView dequeueReusableCellWithIdentifier:@"RHMePriceCell_detail"];
        priceCell.model = model;
        priceCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if (indexPath.row == sectionArr.count-1) {
            [priceCell.bgView setBottomBorder];
        }else{
            [priceCell.bgView setMiddleBorder];
        }
        return priceCell;
    }
    return [UITableViewCell new];
}

-(void)createData
{
    NSArray *datas = @[
    
        @[
            @{@"title":@"尖时电价".rh_localized,
              @"placeHolder":@"请输入充电尖时电价".rh_localized,
              @"isRequired":@(1),
              @"tag":@"pattern_price",
              @"key":@"chargeTip"
            },
            @{@"title":@"峰时电价".rh_localized,
              @"placeHolder":@"请输入充电峰时电价".rh_localized,
              @"isRequired":@(1),
              @"tag":@"pattern_price",
              @"key":@"chargePeak"
            },
            @{@"title":@"平时电价".rh_localized,
              @"placeHolder":@"请输入充电平时电价".rh_localized,
              @"isRequired":@(1),
              @"tag":@"pattern_price",
              @"key":@"chargeFlat"
            },
            @{@"title":@"谷时电价".rh_localized,
              @"placeHolder":@"请输入充电谷时电价".rh_localized,
              @"isRequired":@(1),
              @"tag":@"pattern_price",
              @"key":@"chargeValley"
            }
        ],
        @[
            @{@"title":@"尖时电价".rh_localized,
              @"placeHolder":@"请输入放电尖时电价".rh_localized,
              @"isRequired":@(1),
              @"tag":@"pattern_price",
              @"key":@"dischargeTip"
            },
            @{@"title":@"峰时电价".rh_localized,
              @"placeHolder":@"请输入放电峰时电价".rh_localized,
              @"isRequired":@(1),
              @"tag":@"pattern_price",
              @"key":@"dischargePeak"
            },
            @{@"title":@"平时电价".rh_localized,
              @"placeHolder":@"请输入放电平时电价".rh_localized,
              @"isRequired":@(1),
              @"tag":@"pattern_price",
              @"key":@"dischargeFlat"
            },
            @{@"title":@"谷时电价".rh_localized,
              @"placeHolder":@"请输入放电谷时电价".rh_localized,
              @"isRequired":@(1),
              @"tag":@"pattern_price",
              @"key":@"dischargeValley"
            }
        ],
        @[
            @{@"title":@"时段设置".rh_localized,
              @"isRequired":@(0),
              @"tag":@"pattern_time",
              @"key":@"configJson"
            }
        ]
        
    ];
    
    NSMutableArray *mtbArr = [NSMutableArray array];
    [datas enumerateObjectsUsingBlock:^(NSArray*  _Nonnull arr, NSUInteger idx, BOOL * _Nonnull stop) {
        NSMutableArray *rows = [NSMutableArray array];
        [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            RHMeElectricPricePatternModel *model = [RHMeElectricPricePatternModel yy_modelWithDictionary:obj];
            [rows addObject:model];
        }];
        [mtbArr addObject:rows.copy];
    }];
    
    self.dataArr = mtbArr.copy;
    
    
}
@end
